package com.paishen.peiniwan.frame.widget;

/**
 * Created by peng on 2015/12/7.
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.paishen.peiniwan.R;

public class DashlineView extends View {

    private final Paint paint = new Paint();

    public DashlineView(Context context) {
        this(context, null);
    }

    private int spacing = 36;
    private int mWidth, mHeight;
    private final int d;

    public DashlineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setColor(0xffe7e5e7);

        final float scale = context.getResources().getDisplayMetrics().density;
        spacing =  (int) (12 * scale + 0.5f);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DashlineView);
        d = a.getInt(R.styleable.DashlineView_orientation, 0);

        a.recycle();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        paint.setStrokeWidth(d != 0 ? w : h);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (d != 0) {
            int time = (mHeight / spacing) + 1;
            for (int i = 0; i < time; i++) {
                canvas.drawLine(mWidth / 2, 0 + spacing * i, mWidth / 2, spacing / 2 + spacing * i, paint);
            }
        } else {
            int time =( mWidth / spacing) + 1;
            for (int i = 0; i < time; i++) {
                canvas.drawLine(0 + spacing * i, mHeight / 2, spacing / 2 + spacing * i, mHeight / 2, paint);
            }
        }
    }
}